<template>
  <el-breadcrumb class="breadcrumb_container" separator="/">
    <el-breadcrumb-item v-for="(match, index) in levelList" :key="match.name">
      <span v-if="index + 1 === levelList.length">{{ match.meta.title }}</span>
      <router-link v-else :to="match.path">{{ match.meta.title }}</router-link>
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script>
export default {
  data() {
    return {
      levelList: [],
    };
  },
  watch: {
    $route() {
      this.createBread();
    },
  },
  methods: {
    createBread() {
      let matched = this.$route.matched;
      if (matched.length > 0 && matched[0].name == "home") {
        this.levelList = [{ meta: { title: "首页" }, path: "/index" }];
      } else {
        this.levelList = [
          { meta: { title: "首页" }, path: "/index" },
          ...this.$route.matched,
        ];
      }
    },
  },
  created() {
    this.createBread();
  },
};
</script>

<style lang="less" scoped>
.breadcrumb_container {
  position: fixed;
  width: 100%;
  z-index: 999;
  background-color: #fff;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}
</style>